import Vue from "vue";
import Vuex from "vuex";

Vue.use(Vuex);

import vuexPersist from "vuex-persist";
export default new Vuex.Store({
  state: {
    list: [],
  },
  mutations: {
    setlist(state, val) {
      if (localStorage.getItem("vuex")) {
        state.list = JSON.parse(localStorage.getItem("vuex")).list;
      } else {
        state.list = val;
      }
    },
    change1(state) {
      state.list.forEach((item) => {
        if (item.status == 0) {
          item.status = 1;
        }
      });
    },
    //全部变为已读
    change2(state) {
      state.list.forEach((item) => {
        if (item.status == 1) {
          item.status = 2;
        }
      });
    },
    //全部变为回收站
    change0(state) {
      state.list.forEach((item) => {
        if (item.status == 2) {
          item.status = 0;
        }
      });
    },
    //回收站里的全部变为 未读
  },
  actions: {},
  modules: {},
  getters: {
    obj(state) {
      let num0 = state.list.filter((item) => {
        return item.status == 0;
      }).length;
      let num1 = state.list.filter((item) => {
        return item.status == 1;
      }).length;
      let num2 = state.list.filter((item) => {
        return item.status == 2;
      }).length;

      return { num0: num0, num1: num1, num2: num2 };
    },
  },
  //getters是对vuex进行计算 算出来的数据可以在任何地方使用 this.$store.getters.xx
  plugins: [
    new vuexPersist({
      storage: window.localStorage,
    }).plugin,
  ],
});
